package com.example.webservice.client;

import com.ctc.wstx.evt.WstxEventReader;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.xml.namespace.QName;
import java.beans.XMLDecoder;
import java.io.IOException;
import java.net.HttpCookie;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.http.HttpClient;
import java.util.Scanner;

public class Translate {
    public static void main(String[] args) throws IOException {
        System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.LogFactoryImpl");
// 设置日志打印类
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
//设置默认日志级别
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.simplelog.defaultlog", "error");

        String host = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx/TranslatorReferString?wordKey=${key}";
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要翻译的单词:");
        String text = sc.next();
        String url = host.replace("${key}", text);

        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String results = EntityUtils.toString(entity);
        System.out.println("tranlated: ");
        String []after =results.split("\n");
        for(int i = 2; i + 1 < after.length; i++){
            String tmp = after[i].substring(10);
            tmp = tmp.substring(0,tmp.length() - 10);
            System.out.println(tmp);
        }

        response.close();
    }

}
